package org.client.iot.ui.device.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.lucky.base.network.NetworkApi;
import com.lucky.base.network.observer.BaseObserver;
import com.lucky.utils.FastjsonUtils;
import com.lucky.utils.log.LogUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;

import org.client.iot.R;
import org.client.iot.databinding.FragmentDeviceNetworkBinding;
import org.client.iot.domain.MenuItem;
import org.client.iot.http.ApiService;
import org.client.iot.http.BaseDevReq;
import org.client.iot.ui.device.adapter.DeviceDebugAdapter;
import org.client.iot.ui.device.network.DataClearActivity;
import org.client.iot.ui.device.network.DataSaveActivity;
import org.client.iot.ui.device.network.DataUploadActivity;
import org.client.iot.ui.device.network.ManualNetworkActivity;
import org.tzw.template.base.BaseFragment;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 网络调试
 */
public class DeviceNetworkFragment extends BaseFragment<FragmentDeviceNetworkBinding> {
    private DeviceDebugAdapter adapter;
    private String[] degNetworkMenu;


    @Override
    protected void initData(View view) {
        degNetworkMenu = getResources().getStringArray(R.array.net_option_menu);
        adapter = new DeviceDebugAdapter(requireActivity(), getMenuList());
    }

    @Override
    protected void initView(View view) {
        int spanCount = 3;
        int spacing = 50; // 每个item之间的间距(px)
        boolean includeEdge = true; // 是否在边缘也添加分割线

        mRoot.recNetworkMenu.setLayoutManager(new GridLayoutManager(requireActivity(), spanCount));
        mRoot.recNetworkMenu.addItemDecoration(new DeviceDebugFragment.GridSpacingItemDecoration(spanCount, spacing, includeEdge));
        mRoot.recNetworkMenu.setAdapter(adapter);

//        mRoot.viewPagers.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                return true;
//            }
//        });

    }

    @Override
    protected void initListener() {
        adapter.setOnItemClickListener((adapter, view, position) -> {
            switch (position) {
                case 0: //手动入网
                    requireActivity().startActivity(new Intent(requireActivity(), ManualNetworkActivity.class));
                    break;
                case 1: //数据清除
                    requireActivity().startActivity(new Intent(requireActivity(), DataClearActivity.class));
                    break;
                case 2: //数据上传
                    requireActivity().startActivity(new Intent(requireActivity(), DataUploadActivity.class));
                    break;
                case 3: //离线数据保存
                    requireActivity().startActivity(new Intent(requireActivity(), DataSaveActivity.class));
                    break;
            }
        });
    }

    private List<MenuItem> getMenuList() {
        List<MenuItem> menuList = new ArrayList<>();
        menuList.add(new MenuItem(degNetworkMenu[0], R.drawable.network_ico_manual_network));
        menuList.add(new MenuItem(degNetworkMenu[1], R.drawable.network_ico_data_clear));
        menuList.add(new MenuItem(degNetworkMenu[2], R.drawable.network_ico_upload_data));
        menuList.add(new MenuItem(degNetworkMenu[3], R.drawable.network_ico_save_data));
        return menuList;
    }

}